package tv.scolopender.asynctask;

import java.util.ArrayList;

import tv.scolopender.adapter.ScoloArrayAdapter;
import tv.scolopender.json.FavouriteJSON;
import tv.scolopender.model.Scolo;
import tv.scolopender.model.User;
import tv.scolopender.runnable.UpdateScoloAdapter;
import android.app.Activity;
import android.os.AsyncTask;
import android.provider.Settings.Secure;

public class ScoloLikesAsyncTask extends
		AsyncTask<Integer, Void, ArrayList<Scolo>> {
	private final Activity activity;
	private final ScoloArrayAdapter scoloAdapter;

	public ScoloLikesAsyncTask(Activity activity, ScoloArrayAdapter scoloAdapter) {
		super();
		this.activity = activity;
		this.scoloAdapter = scoloAdapter;
	}

	public ScoloLikesAsyncTask(Activity activity) {
		this(activity, null);
	}

	@Override
	protected ArrayList<Scolo> doInBackground(Integer... params) {
		int offset = params[0];
		int amount = params[1];
		User user = new User(Secure.getString(activity.getContentResolver(),
				Secure.ANDROID_ID));
		ArrayList<Scolo> scolos = FavouriteJSON.selectFavourites(user, offset,
				amount);
		if (scoloAdapter == null) {
			UpdateScoloAdapter updateScoloAdapter = new UpdateScoloAdapter(
					scoloAdapter, scolos, activity);
			activity.runOnUiThread(updateScoloAdapter);
		}
		return scolos;
	}

}
